//
//  USUnmatchController.m
//  us
//
//  Created by karry on 2025/4/8.
//

#import "USUnmatchController.h"

@interface USUnmatchController ()

@end

@implementation USUnmatchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"解除匹配";
    [self setupUI];
}

- (void)setupUI{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Group 20"]];
    [self.view addSubview:imageView];

    UILabel *titleLab = [UILabel new];
    titleLab.text = @"注意事项";
    titleLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    titleLab.textColor = UIColor.secondaryLabelColor;
    [self.view addSubview:titleLab];
    
    UILabel *infoLab = [UILabel new];
    infoLab.text = @"1. 解除匹配后，数据将全部删除，包括您记录的所有数据，包括聊天，相册，日记，购买的商品等。\n\n2. 解除匹配后重新匹配，不会恢复之前的数据。\n\n3. 根据法律规定，某些情况我们可能需要保留数据一段时间，以符合法规。";
    infoLab.font = [UIFont systemFontOfSize:15];
    infoLab.numberOfLines = 0;
    infoLab.textColor = UIColor.secondaryLabelColor;
    [self.view addSubview:infoLab];

    UIButton *actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [actionBtn addTarget:self action:@selector(readAction:) forControlEvents:UIControlEventTouchUpInside];
    [actionBtn setBackgroundImage:[UIImage imageNamed:@"login_unagree"] forState:0];
    [actionBtn setImage:nil forState:0];
    [actionBtn setImage:[UIImage imageNamed:@"login_agree"] forState:UIControlStateSelected];
    [self.view addSubview:actionBtn];
    
    UILabel *readLab = [UILabel new];
    readLab.text = @"我已经阅读并同意以上注意事项，同意解除匹配";
    readLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
    [self.view addSubview:readLab];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop).mas_offset(30);
    }];
    
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.top.mas_equalTo(imageView.mas_bottom).mas_offset(30);
        make.right.mas_offset(-16);
    }];
    
    [infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(titleLab);
        make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(6);
    }];
    
    [actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleLab);
        make.top.mas_equalTo(infoLab.mas_bottom).mas_offset(60);
    }];
    
    [readLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(actionBtn.mas_right).mas_offset(6);
        make.centerY.mas_equalTo(actionBtn);
        make.right.mas_offset(-16).priority(250);
    }];
    
    UIButton *sureBtn = [UIButton us_fillButtonWithTitle:@"确定解除匹配"];
    [sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    
    UIButton *cancelBtn = [UIButton us_borderButtonWithTitle:@"取消"];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.height.mas_equalTo(UIButton.us_buttonDefaultHeight);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(-20);
    }];
    
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(cancelBtn.mas_right).mas_offset(20);
        make.right.mas_offset(-20);
        make.width.height.centerY.mas_equalTo(cancelBtn);
        
    }];
    
    RAC(sureBtn, enabled) = [RACObserve(actionBtn, selected) distinctUntilChanged];

}

- (void)cancelAction:(id)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)sureAction:(id)sender{
    
    [USFeedbackTool notifyWarning];

    MOAlertBaseView *alert = [[MOAlertBaseView alloc]initWithTitle:@"您确定要解除匹配吗？" detailTitle:@"你即将解除与你的另一半的匹配关系，所有共同的记录和记忆将被删除。或许你们可以慎重考虑你们的感情，但我们尊重您的决定" contentView:nil leftButtonImageName:nil rightButtonImageName:nil autoDissmiss:YES tapDissmiss:YES];
    @weakify(self)
    alert.didClickIndexBlock = ^(NSInteger index) {
        @strongify(self)
        if (index == 1) {
            [self postCancelAction];
        }
    };
    alert.titleLabel.textColor = UIColor.us_redColor;
    [alert show];
    
}

 
- (void)postCancelAction{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    [params setValue:USUser.user.userId forKey:@"myselfUserId"];
    [params setValue:USUser.user.relUserId forKey:@"relUserId"];
 
    [[EMNetWork shareManager]em_post:API_RemoveRelation params:params hud:@"" showError:YES success:^(EMApiModel *api) {
        if (api.success == NO) {
            return;
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:USNotificationMatch object:nil];

        [self.view makeToast:@"解除匹配成功"];
        
        [USIMCmdCustomManager imSendCmdWithAction:USIMCmdAction_UnMatch andExt:@{}];

#pragma mark - warning
        //删除本地所以环信消息
        [[[EMClient sharedClient].chatManager getConversationWithConvId:USUser.user.relImUserName] deleteAllMessages:nil];
        
        [[EMClient sharedClient] logout:YES];//同时退出环信（避免解除后收到环信消息）
        
        [self.navigationController popToRootViewControllerAnimated:YES];

     
    } fail:^{
        
    }];
    
}

- (void)readAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    [USFeedbackTool impactLight];
}


@end
